/**
 * Copyright @ 2000 ysten Co. Ltd. 
 * All right reserved.     
 * @author: wangmin
 * date: 2016-05-31     
 */
package com.task.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

/**
 * 属性工具
 */
public class PropertiesUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesUtil.class);

    /**
     * 微信属性文件名称.
     */
    private final static String PROPERTIES_FILENAME = "weixin.properties";
    /**
     * 配置属性对象静态化
     */
    private static Properties properties;

    public static Properties getProperties() {
        if (properties == null) {
            synchronized (PropertiesUtil.class) {
                if (properties == null) {
                    loadProperties(PROPERTIES_FILENAME);
                }
            }
        }
        return properties;
    }

    /**
     * 根据key获取配置的字符串value值
     *
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        if (key == null) {
            return null;
        }
        return getProperties().getProperty(key);
    }

    public static void setProperty(String key,String value) {
        if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(value)) {
            getProperties().setProperty(key,value);

        }
    }

    /**
     * 更新属性文件
     */
    public static void updateProperties(){
        OutputStream fos = null;
        try {
            fos = new FileOutputStream(new File(Thread.currentThread().getContextClassLoader().getResource(PROPERTIES_FILENAME).getFile()));
            getProperties().store(fos, "update value");
        } catch (IOException e) {
            LOGGER.error("udpate value error :{}", e);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    LOGGER.error("close error :{}", e);
                }
            }
        }

    }

    /**
     * 根据key获取配置的数字value值
     *
     * @param key
     * @return
     */
    public static int getNumerProperty(String key) {
        String value = getProperties().getProperty(key);
        if (NumberUtils.isNumber(value)) {
            return Integer.parseInt(value);
        } else {
            return 0;
        }
    }

    public static void loadProperties(Properties props) {
        properties = props;
    }

    /**
     * 根据指定的文件名称，从类路径中加载属性文件，构造Properties对象
     *
     * @param filename 属性文件名称
     */
    public static void loadProperties(String filename) {
        InputStream in = null;
        ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
        properties = new Properties();
        if (threadContextClassLoader != null) {
            in = threadContextClassLoader.getResourceAsStream(filename);
        }
        if (in == null) {
            in = PropertiesUtil.class.getResourceAsStream(filename);
            if (in == null) {
                LOGGER.warn("No properties file found in the classpath by filename " + filename);
            }
        } else {
            try {
                properties.load(in);
                LOGGER.info("Properties read " + properties);
            } catch (Exception e) {
                LOGGER.error("Error reading from " + filename, e);
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                    LOGGER.error("IOException while closing InputStream: " + e.getMessage());
                }
            }
        }
    }
}
